import { Message } from "element-ui";
import merge from "lodash.merge";
import { quillEditor } from "vue-quill-editor";
import "quill/dist/quill.core.css";
import "quill/dist/quill.snow.css";
import "quill/dist/quill.bubble.css";
import store from "@/config/store";
import util from "@/config/util";
import { quillRedefine } from "./vue-quill-editor-upload";
import "./style.less";

const defaultConfig = {
  uploadConfig: {
    headers: {
      token: store.state.token
    },
    action: "",  // 必填参数 图片上传地址
    methods: "POST",  // 必填参数 图片上传方式
    accept: "image/png, image/gif, image/jpeg, image/bmp, image/x-icon",  // 可选 可上传的图片格式
    name: "file",  // 必填参数 文件的参数名
    size: false,  // 可选参数   图片大小，单位为Kb, 1M = 1024Kb
    res(result) {
      if (!result.success) {
        Message.error(result.msg || "上传错误");
        return;
      }
      return result.data;
    },
    start: util.emptyFn,
    end: util.emptyFn,
    error: util.emptyFn,
    success: util.emptyFn
  },
  placeholder: "请输入...",
  theme: "snow",  // 主题
  modules: {
    toolbar: {
      // 工具栏选项
      container: [
        ["bold", "italic", "underline", "strike"],
        ["blockquote", "code-block"],
        [{ "header": 1 }, { "header": 2 }],
        [{ "list": "ordered" }, { "list": "bullet" }],
        [{ "script": "sub" }, { "script": "super" }],
        [{ "indent": "-1" }, { "indent": "+1" }],
        [{ "direction": "rtl" }],
        [{ "size": ["small", false, "large", "huge"] }],
        [{ "header": [1, 2, 3, 4, 5, 6, false] }],
        [{ "color": [] }, { "background": [] }],
        [{ "font": [] }],
        [{ "align": [] }],
        ["clean"],
        ["link", "image", "video"]
      ]
    }
  }
};

export default {
  name: "editor",
  functional: true,
  render: (createElement, context) => {
    const { options = {} } = context.data.attrs || {};
    merge(context.data, {
      attrs: {
        options: quillRedefine(merge({}, defaultConfig, options))
      }
    });
    return createElement(quillEditor, context.data, context.children);
  }
};
